//
//  LKMainViewController.swift
//  LKweibo
//
//  Created by mac on 16/4/4.
//  Copyright © 2016年 什么人玩什么鸟. All rights reserved.
//

import UIKit

class LKMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        tabBar.backgroundColor = UIColor.redColor()
        //1加载子控制器
        addChiledControllers()
        //2加载撰写按钮
        addCompileButton()
    }
    //撰写按钮
    private lazy var compileButton: UIButton = UIButton(bgImgName: "tabbar_compose_button", ImgName: "tabbar_compose_icon_add")
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        tabBar.bringSubviewToFront(compileButton)
    }
    
    //撰写按钮点击事件@objc
    @objc private func didcompileButton(){
        print("点击事件")
    }
    
}

extension LKMainViewController{
    
    private func addCompileButton(){
        
        tabBar.addSubview(compileButton)
        let count = childViewControllers.count
        let w = tabBar.bounds.width / CGFloat(count) - 1
        compileButton.frame = CGRectInset(tabBar.bounds, 2 * w, 1)
        compileButton.addTarget(self, action: "didcompileButton", forControlEvents: .TouchUpInside)
        
    }
    
    private func addChiledControllers(){
        
        tabBar.tintColor = UIColor.orangeColor()
        
        //首页
        addChiledController(LKHomeTableViewController(), title: "首页", itemImg: "tabbar_home")
        //消息
        addChiledController(LKHomeTableViewController(), title: "消息", itemImg: "tabbar_message_center")
        //占位框
        addChildViewController(UIViewController())
        //发现
        addChiledController(LKHomeTableViewController(), title: "发现", itemImg: "tabbar_discover")
        //我
        addChiledController(LKHomeTableViewController(), title: "我", itemImg: "tabbar_profile")
        
    }
    
    //创建,多态体现
    private func addChiledController(vc:UIViewController, title:String, itemImg:String){

        vc.title = title
        vc.tabBarItem.image = UIImage(named: itemImg)
        let nv = UINavigationController(rootViewController: vc)
        addChildViewController(nv)

    }
    
    
}
